import 'dart:collection';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:opene/app_constant.dart';
import 'package:opene/app_theme.dart';
import 'package:opene/app_utils.dart';
import 'package:opene/base/base_state.dart';
import 'package:opene/base/loading.dart';
import 'package:opene/net/method/base_method.dart';
import 'package:opene/net/network_manager.dart';
import 'package:opene/net/response/base_response.dart';
import 'package:opene/ui/b2blabel/b2blabellpnscreen.dart';
import 'package:opene/ui/b2blabel/b2blabelmodel.dart';
import 'package:opene/ui/b2cpick/b2cpickmodel.dart';
import 'package:opene/ui/b2cpick/b2cpickoblpnscreen.dart';
import 'package:opene/ui/tally/tallymodel.dart';
import 'package:opene/ui/tally/tallyskuscreen.dart';
import 'package:toast/toast.dart';

class B2BLabelScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return B2BLabelScreenPage();
  }
}

class B2BLabelScreenPage extends StatefulWidget {
  @override
  _B2BLabelScreenPageState createState() => _B2BLabelScreenPageState();
}

class _B2BLabelScreenPageState extends BaseState<B2BLabelScreenPage> {
  final TextEditingController dateController = TextEditingController();
  final controller = ScrollController();

  int index = 0, position;
  List<B2BLabelBean> data = new List();

  @override
  String setTitle({String title}) {
    return !Utils.isEmpty(title) ? title : "B2BLabel";
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    Future.delayed(Duration(milliseconds: 300), () {
      setState(() {
        getTallyContainerNo();
      });
    });
  }

  @override
  Widget getViewBinding() {
    return Container(
      margin: EdgeInsets.only(left: 15, right: 10, top: 10),
      child: setListView(),
    );
  }

  ListView setListView() {
    return ListView.builder(
      shrinkWrap: true,
      // physics: NeverScrollableScrollPhysics(),
      itemCount: data == null ? 0 : data.length,
      itemBuilder: (context, one) {
        B2BLabelBean b2cPickBean = data[one];
        if (b2cPickBean.click == null) {
          b2cPickBean.click = false;
        }
        return Column(
          children: [
            Row(
              children: [
                Expanded(
                    flex: 1,
                    child: GestureDetector(
                      onTap: () {
                        checkB2b(b2cPickBean.jobId);
                      },
                      child: Text(
                      "${b2cPickBean.refId}-${b2cPickBean.jobId}(${b2cPickBean.type})",
                      style: MyAppTheme.test12,
                    ),)),
                GestureDetector(
                  onTap: () {
                    setState(() {
                      if(b2cPickBean.click){
                        b2cPickBean.click = !b2cPickBean.click;
                        return;
                      }
                      b2bOrderLabelDetails(b2cPickBean.jobId, one);
                    });
                  },
                  child: Icon(
                    b2cPickBean.click ? Icons.keyboard_arrow_up : Icons.keyboard_arrow_down,
                    size: 20,
                    color: MyAppTheme.nearlyBlue,
                  ),
                ),
              ],
            ),
            Visibility(visible: b2cPickBean.click ? true : false, child: Row(
              children: [
                SizedBox(width: 5,),
                Expanded(
                  flex: 1,
                  child: Text(
                    "SKU",
                    style: MyAppTheme.text_11_333333,
                  ),),
                Expanded(
                  flex: 1,
                  child: Text(
                    "Picked",
                    style: MyAppTheme.text_11_333333,
                  ),),
                Expanded(
                  flex: 1,
                  child: Text(
                    "Pending",
                    style: MyAppTheme.text_11_333333,
                  ),),
              ],
            )),
            Visibility(visible: b2cPickBean.click ? true : false, child: setListItemView(one, b2cPickBean.list)),
            Divider(),
          ],
        );
      },
    );
  }

  ListView setListItemView(int one, List<B2BLabelDetails> childPicks) {
    return ListView.builder(
      shrinkWrap: true,
      // physics: NeverScrollableScrollPhysics(),
      itemCount: childPicks == null ? 0 : childPicks.length,
      itemBuilder: (context, two) {
        B2BLabelDetails childPick = childPicks[two];
        return Row(
          children: [
            SizedBox(width: 5,),
            Expanded(
                flex: 1,
                child: Text(
                  Utils.getStringisEmpty(childPick.sku),
                  style: MyAppTheme.text_11_333333,
                ),),
            Expanded(
                flex: 1,
                child: Text(
                  Utils.getStringisInteger(childPick.qty),
                  style: MyAppTheme.text_11_333333,
                ),),
            Expanded(
                flex: 1,
                child: Text(
                  Utils.getStringisInteger(childPick.pending),
                  style: MyAppTheme.text_11_333333,
                ),),
          ],
        );
      },
    );
  }

  @override
  void setNext() {
  }

  @override
  void setReset() {
    setState(() {
    });
  }

  void getTallyContainerNo() async {
    try {
      // 显示加载中对话框
      LoadingIndicator.show(context);
    } catch (e) {
      e.toString();
    }

    BaseResponse baseResponse = await NetworkManager().request(context, Constants.b2bPendingJobs);

    try {
      // 在需要的时候隐藏加载中对话框
      LoadingIndicator.hide();
    } catch (e) {
      e.toString();
    }

    if (baseResponse.status == 200) {
      setState(() {
        data = baseResponse.data == null ? null : (new List<dynamic>.from(baseResponse.data).map((userJson) => B2BLabelBean.fromJson(userJson)).toList());
      });
    } else {
      ToastContext().init(context);
      Toast.show(Utils.getStringisEmpty(baseResponse.msg), duration: Toast.lengthShort, gravity: Toast.bottom);
    }
  }
  void b2bOrderLabelDetails(String jobId, int position) async {
    try {
      // 显示加载中对话框
      LoadingIndicator.show(context);
    } catch (e) {
      e.toString();
    }

    BaseResponse baseResponse = await NetworkManager().request(context, Constants.b2bOrderLabelDetails+"/"+jobId);

    try {
      // 在需要的时候隐藏加载中对话框
      LoadingIndicator.hide();
    } catch (e) {
      e.toString();
    }

    if (baseResponse.status == 200) {
      setState(() {
        List<B2BLabelDetails> b2BLabelDetails = baseResponse.data == null ? null : (new List<dynamic>.from(baseResponse.data).map((userJson) => B2BLabelDetails.fromJson(userJson)).toList());
        if(b2BLabelDetails==null){
          return;
        }
        B2BLabelBean b2BLabelBean = data[position];
        b2BLabelBean.click = true;
        b2BLabelBean.list = b2BLabelDetails;
        data[position] = b2BLabelBean;

      });
    } else {
      ToastContext().init(context);
      Toast.show(Utils.getStringisEmpty(baseResponse.msg), duration: Toast.lengthShort, gravity: Toast.bottom);
    }
  }

  void checkB2b(String order) async {
    try {
      // 显示加载中对话框
      LoadingIndicator.show(context);
    } catch (e) {
      e.toString();
    }

    BaseResponse baseResponse = await NetworkManager().request(context, Constants.checkB2b+"/"+order);

    try {
      // 在需要的时候隐藏加载中对话框
      LoadingIndicator.hide();
    } catch (e) {
      e.toString();
    }

    if (baseResponse.status == 200) {

      B2BLabelLpn b2bLabelLpn = B2BLabelLpn.fromJson(baseResponse.data);
      Navigator.push(
        context,
        MaterialPageRoute(builder: (context) => B2BLabelLPNScreen(order:order,b2bLabelLpn: b2bLabelLpn)),
      ).then((value) => {getTallyContainerNo()});
    } else {
      ToastContext().init(context);
      Toast.show(Utils.getStringisEmpty(baseResponse.msg), duration: Toast.lengthShort, gravity: Toast.bottom);
    }
  }

  @override
  void showFileUploadData(String path, int type, int position, bool isSubmit) {
    // TODO: implement showFileUploadData
  }
}
